package ru.poomsae.isap.Online.Hapki

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.example.isap_wtf.Model.ApiEndPoint
import com.example.isap_wtf.Model.ParametrsISAP
import com.example.isap_wtf.Model.zaprosJSON
import ru.poomsae.isap.Offline.MenuActivity
import ru.poomsae.isap.Online.FreestyleHosinsul.HapkiPresentation
import ru.poomsae.isap.R

class Hapki  : AppCompatActivity() {

    var hie = 0
    var num = 0
    var ang = 0
    var spa = 0
    var acr = 0
    var bas = 0

    var accy = 0.0
    var accyInt = 0

    var min01 = 0
    var min03 = 0

    var url = ""
    var mIDHost: String = ParametrsISAP.idHostServer
    var onlineConSys = ParametrsISAP.onlineConSys
    lateinit var myzaprosJSON: zaprosJSON
    lateinit var myTextView: TextView
    lateinit var myLinearLayout: LinearLayout
    lateinit var myRelativeLayout: RelativeLayout
    lateinit var mySudTextView: TextView
    lateinit var myPoomsaeTextView: TextView
    var idvistuplenie = "null"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.freestyle_hapkido)

        url = "http://" + mIDHost + ApiEndPoint.VISTUPLENIE

        myTextView = findViewById(R.id.idvistFreeText) as TextView
        myLinearLayout = findViewById(R.id.progressBarFree) as LinearLayout
        myRelativeLayout= findViewById(R.id.freestyle) as RelativeLayout
        mySudTextView = findViewById(R.id.sudNumFreeTV) as TextView
        myPoomsaeTextView = findViewById(R.id.poomsaeFreeTV) as TextView

        val textHapkiView1 = findViewById<TextView>(R.id.textHapkiView1)
        val textHapkiView2 = findViewById<TextView>(R.id.textHapkiView2)
        val textHapkiView3 = findViewById<TextView>(R.id.textHapkiView3)
        val textHapkiView4 = findViewById<TextView>(R.id.textHapkiView4)
        val textHapkiView5 = findViewById<TextView>(R.id.textHapkiView5)
        val textHapkiView6 = findViewById<TextView>(R.id.textHapkiView6)

        when (ParametrsISAP.hapki) {
            1 -> {
                textHapkiView1.text = (getString(R.string.udary_zach))
                textHapkiView2.text = (getString(R.string.udar_nogam_pr))
                textHapkiView3.text = (getString(R.string.udar_vrash))
                textHapkiView4.text = (getString(R.string.navik))
                textHapkiView5.text = (getString(R.string.peredvijenie))
                textHapkiView6.text = (getString(R.string.hapki_acro))
            }
            3 ->  {
                textHapkiView1.text = (getString(R.string.brosok_teh))
                textHapkiView2.text = (getString(R.string.razbiv_predm))
                textHapkiView3.text = (getString(R.string.slojny_udar_nog))
                textHapkiView4.text = (getString(R.string.navik_oruj))
                textHapkiView5.text = (getString(R.string.dinamik_pered))
                textHapkiView6.text = (getString(R.string.hapki_acro))
            }
        }

        myzaprosJSON = zaprosJSON(myTextView, myLinearLayout, myRelativeLayout, mySudTextView, myPoomsaeTextView)
        val decorView = window.decorView
        val uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN

        if(onlineConSys == 1) { myzaprosJSON.waitVistup(); visibleSystem(); decorView.systemUiVisibility = uiOptions; getSupportActionBar()?.hide() }

        val height = findViewById<RadioGroup>(R.id.height)
        val heigFree = findViewById<TextView>(R.id.heigFree)
        val accyrFree = findViewById<TextView>(R.id.accyrFree)
        height.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                hie = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                accy = (hie + num + ang + spa + acr + bas).toDouble()/(10).toDouble()
                heigFree.setText(radio.text as String)
                accyrFree.setText(String.format("%.1f",accy))
            })

        val kick = findViewById<RadioGroup>(R.id.kick)
        val numkickFree = findViewById<TextView>(R.id.numkickFree)
        kick.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                num = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                accy = (hie + num + ang + spa + acr + bas).toDouble()/(10).toDouble()
                numkickFree.setText(radio.text as String)
                accyrFree.setText(String.format("%.1f",accy))
            })

        val angle = findViewById<RadioGroup>(R.id.angle)
        val angleFree = findViewById<TextView>(R.id.angleFree)
        angle.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                ang = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                accy = (hie + num + ang + spa + acr + bas).toDouble()/(10).toDouble()
                angleFree.setText(radio.text as String)
                accyrFree.setText(String.format("%.1f",accy))
            })

        val sparing = findViewById<RadioGroup>(R.id.sparing)
        val sparFree = findViewById<TextView>(R.id.sparFree)
        sparing.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                spa = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                accy = (hie + num + ang + spa + acr + bas).toDouble()/(10).toDouble()
                sparFree.setText(radio.text as String)
                accyrFree.setText(String.format("%.1f",accy))
            })

        val acrobatic = findViewById<RadioGroup>(R.id.acrobatic)
        val acrFree = findViewById<TextView>(R.id.acrFree)
        acrobatic.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                acr = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                accy = (hie + num + ang + spa + acr + bas).toDouble()/(10).toDouble()
                acrFree.setText(radio.text as String)
                accyrFree.setText(String.format("%.1f",accy))
            })

        val basic = findViewById<RadioGroup>(R.id.basic)
        val basFree = findViewById<TextView>(R.id.basFree)
        basic.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                bas = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                accy = (hie + num + ang + spa + acr + bas).toDouble()/(10).toDouble()
                basFree.setText(radio.text as String)
                accyrFree.setText(String.format("%.1f",accy))
            })

        val btnPresentationFree = findViewById<Button>(R.id.btnPresentationFree)
        btnPresentationFree.setOnClickListener {
            val intent = Intent (this, HapkiPresentation::class.java)
            intent.putExtra("accuracyDoubleFree", accy)
            startActivity(intent)
        }


    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_off, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item?.itemId) {

            R.id.menu_offline -> {
                val intent = Intent (this, MenuActivity::class.java)
                startActivity(intent)
            }

        }

        return true
    }

    @SuppressLint("WrongViewCast")
    fun visibleSystem() {
        if (idvistuplenie == "null") {
            val freestyle = findViewById<LinearLayout>(R.id.freestyle)
            val progressBarFree = findViewById<ProgressBar>(R.id.progressBarFree)
            val idvistFreeText = findViewById<TextView>(R.id.idvistFreeText)
            freestyle.visibility = View.GONE
            progressBarFree.visibility = View.VISIBLE
            idvistFreeText.setText(getString(R.string.idvistTextView))
        }
    }
}